package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.ImportStatusPojo;
import com.xbongbong.paas.redis.RedisCacheAutoConfiguration;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 导入相关特殊redis操作helper
 * @author 魏荣杰
 * @date 2019/12/5 8:29 下午
 * @since v4.6.1
 * @version v4.6.1
 */
@Component
public class ImportRedisHelper {
    private static final Logger LOG = LoggerFactory.getLogger(ImportRedisHelper.class);

    /**
     * 项目名，redis的第一级前缀
     */
    @Value("${com.xbongbong.pro.main.name}")
    private String projectName;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisCacheAutoConfiguration redisCacheAutoConfiguration;

    /**
     * Description: 更新导入状态
     * @param key
     * @param importStatusPojo
     * @param decrNum
     * @author 魏荣杰
     * @date 2019/12/5 8:51 下午
     * @since v4.6.1
     */
    public ImportStatusPojo updateImportStatus(String key, ImportStatusPojo importStatusPojo, Integer decrNum) {
        List<String> preStrs = Arrays.asList(RedisPrefixConstant.IMPORT_STATUS_KEY);
        List<String> keys = new ArrayList<>();
        for (String pre : preStrs) {
            keys.add(projectName + ":" + pre + ":" + key);
        }
        String result = stringRedisTemplate.execute(redisCacheAutoConfiguration.redisImportStatusUpdateScript(), keys, JSON.toJSONString(importStatusPojo), String.valueOf(decrNum));
        return JSON.parseObject(result, ImportStatusPojo.class);
    }

    /**
     * Description: 更新导入的错误，覆盖，无签订人缓存
     * @param prefix
     * @param key
     * @param updateList
     * @author 魏荣杰
     * @date 2019/12/9 10:06
     * @version v4.6.1
     */
    public void updateImportList(String prefix, String key, List updateList) {
        String keys = projectName + ":" + prefix + ":" + key;
        List<String> list = new ArrayList<>(updateList.size());
        for (Object obj : updateList) {
            list.add(JSON.toJSONString(obj));
        }
        stringRedisTemplate.opsForList().rightPushAll(keys, list);
    }

    public void updateImportList(String prefix, String key, List updateList, int time) {
        String keys = projectName + ":" + prefix + ":" + key;
        updateImportList(prefix, key, updateList);
        if (time > 0) {
            stringRedisTemplate.expire(keys, time, TimeUnit.SECONDS);
        }
    }

    /**
     * Description: 获取导入的错误，覆盖，无签订人等数据
     * @param prefix
     * @param key
     * @return java.util.List<java.lang.String>
     * @throws
     * @author 魏荣杰
     * @date 2019/12/9 16:06
     * @version
     * @since
     */
    public List<String> getImportList(String prefix, String key) {
        String keys = projectName + ":" + prefix + ":" + key;
        return stringRedisTemplate.opsForList().range(keys, 0, -1);
    }

    /**
     * Description: 获取导入错误，覆盖，无签订人等数据的大小
     * @param prefix
	 * @param key
     * @return java.lang.Long
     * @throws
     * @author 魏荣杰
     * @date 2022/11/30 09:41
     * @since
     */
    public Long getImportListSize(String prefix, String key) {
        String keys = projectName + ":" + prefix + ":" + key;
        return stringRedisTemplate.opsForList().size(keys);
    }

    /**
     * Description: 获取导入list
     * @param prefix
	 * @param key
	 * @param start
	 * @param end
     * @return java.util.List<java.lang.String>
     * @throws
     * @author 魏荣杰
     * @date 2022/11/30 10:15
     * @since
     */
    public List<String> getImportList(String prefix, String key, Long start, Long end) {
        String keys = projectName + ":" + prefix + ":" + key;

        return stringRedisTemplate.opsForList().range(keys, start, end);
    }

    /**
     * Description: 减少导入的分片总量
     * @param key
     * @param num
     * @return java.lang.Long
     * @author 魏荣杰
     * @date 2019/12/16 3:54 下午
     */
    public Long decrImportTotalFragmentation(String key, Long num) {
        List<String> keys = Arrays.asList(projectName + ":" + RedisPrefixConstant.IMPORT_STATUS_KEY + ":" + key);
        return stringRedisTemplate.execute(redisCacheAutoConfiguration.redisImportFragmentationNumDecrScript(), keys, String.valueOf(num));
    }


    /**
     * Description: 判断导入数据是否重复
     * @param key
     * @param value
     * @return java.lang.Boolean
     * @throws
     * @author 魏荣杰
     * @date 2021/1/27 16:33
     * @since
     */
    public Boolean checkImportRepeat(String key, String value) {
        List<String> keys = new ArrayList<>();
        List<String> preStrs = Collections.singletonList(RedisPrefixConstant.IMPORT_REPEAT_KEY);
        for (String preStr : preStrs) {
            keys.add(projectName + ":" + preStr + ":" + key);
        }
        return stringRedisTemplate.execute(redisCacheAutoConfiguration.redisImportRepeatScript(), keys, value);
    }

    /**
     * 导入结束后移除数据
     * @author 魏荣杰
     * @date 2021/1/29 14:04
     * @since
     * @version
     */
    public void removeCheckRepeatValue(String key, Set<String> value) {
        String keys = projectName + ":" + RedisPrefixConstant.IMPORT_REPEAT_KEY + ":" + key;
        stringRedisTemplate.opsForSet().remove(keys, value.toArray());
    }

}
